﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using Negocio;
using Dominio;
using System.Web.UI.WebControls;

public partial class InserirNovaTurma : System.Web.UI.Page
{
    TurmaVO vo;
    protected void Page_Load(object sender, EventArgs e)
    {
        if ((Request.QueryString.Count != 0) && (!IsPostBack))
        {
            vo = TurmaNegocio.findByPrimaryKey(Convert.ToInt32(Request.QueryString["id"]));
            this.objetoToForm(vo);
        }
       
    }

    private void objetoToForm(TurmaVO turma)
    {
        lblID.Text = turma.id.ToString();
        txtNome.Text = turma.Nome;
        txtSerie.Text = turma.Serie;
        txtAno.Text = turma.Ano.ToString();
        if (turma.Turno == "Matutino") ddlTurno.SelectedIndex = 0; else ddlTurno.SelectedIndex = 1;
    }

    private void formToObjecto()
    {
        if (vo == null)
            vo = new TurmaVO();

        try
        {
            vo.id = Convert.ToInt32(lblID.Text);
        }
        catch (Exception)
        {
            vo.id = 0;
        }
        vo.Nome = txtNome.Text;
        vo.Serie = txtSerie.Text;
        vo.Ano = Convert.ToInt32(txtAno.Text);
        vo.Turno = ddlTurno.SelectedItem.Text.ToString();

    }



    protected void Button1_Click(object sender, EventArgs e)
    {
        this.formToObjecto();
        TurmaNegocio.salvar(vo);
        Response.Redirect("Turma.aspx");
    }
    
    protected void Button2_Click1(object sender, EventArgs e)
    {
        Response.Redirect("Turma.aspx");
    }
}